<?php
namespace App\Controllers;
class Travel extends BaseController
{
    public function index()
    {
        // connect to the model
    $places = new \App\Models\Places();
    // retrieve all the records
    $records = $places->findAll();
    // get a template parser
    $parser = \Config\Services::parser();
    // tell it about the substitions
    return $parser->setData(['records' => $records])
    // and have it render the template with those
    ->render('placeslist');
    }

public function showme($id)  
{    
    $places = new \App\Models\Places();  
    
    $record = $places->find($id);  
    if (!$record) {  
        throw new \CodeIgniter\Exceptions\PageNotFoundException("Place with ID $id not found.");  
    }   
    $table = new \CodeIgniter\View\Table();  

    $table->addRow(anchor("travel/edit/$id","Edit"));

    $table->addRow('ID', $record['id']);
    $table->addRow('Img', '<img src="/image/' . $record['img'] . '" alt="' . $record['name'] . ' image" style="max: width 450px;px;"/>');   
    $table->addRow('Name', $record['name']);  
    $table->addRow('Description', $record['description']); 
    $table->addRow('Race', $record['race']); 
    $table->addRow('Food that one enjoys', $record['food that one enjoys']); 
    $table->addRow('Hobby', $record['hobby']);  
     $table->addRow('Link', '<a href="' . $record['link'] . '" target="_blank">' . $record['link'] . '</a>'); 
    $table->addRow('Image', '<img src="/image/' . $record['image'] . '" alt="' . $record['name'] . ' image" style="max: width 450px;px;"/>'); 

    $template = [  
        'table_open' => '<table cellpadding="5px">',  
        'cell_start' => '<td style="border: 1px solid #dddddd;">',  
        'row_alt_start' => '<tr style="background-color:#dddddd">',  
    ];  

    $table->setTemplate($template);  
    $fields = [  
        'title' => 'Role Introduction Details',  
        'heading' => ' ' . $record['name'],  
        'footer' => 'Copyright GuanghengHuang'  
    ];  
    
    $parser = \Config\Services::parser();  
    return $parser->setData($fields)  
        ->render('templates/top') .  
        $table->generate() .  
        $parser->setData($fields)  
        ->render('templates/bottom');  
}

public function edit($id = NULL) { 
    $places = new \App\Models\Places();
     $record = $places->find($id);
     $fields = [
        'title' => 'Edit a Destination',
        'heading' => 'Edit a Destination',
        'footer' => 'Copyright GuanghengHuang'
      ];

      $parser = \Config\Services::parser();
         return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
             ->render('editplace') .
            $parser->setData($fields)
            ->render('templates\bottom');
}

public function handle() { 
    $places = new \App\Models\Places();
    helper(['form', 'url']);
    $record = $places->find($this->request->getPost('id'));

    if ($this->validate($places->validationRules)) {
                    $places->save([
                        
                        'id' => $this->request->getPost('id'),
                        'img' => $this->request->getPost('img'),
                        'name' => $this->request->getPost('name'),
                         'description' => $this->request->getPost('description'),
                         'race' => $this->request->getPost('race'),
                         'food that one enjoys' => $this->request->getPost('food that one enjoys'), 
                         'hobby' => $this->request->getPost('hobby'),
                         'link' => $this->request->getPost('link'),
                        'image' => $this->request->getPost('image'),
            ]);
         }
                    $fields = [
                        'title' => 'Edit a Destination',
                        'heading' => 'Edit a Destination',
                         'footer' => 'Copyright GuanghengHuang'
                        ];
                     $parser = \Config\Services::parser(); 
                     //Top of page
        $returnView = $parser->setData($fields)->render('templates\top');
         //Body of page
         if (!$this->validate($places->validationRules)) {
         $returnView .= $parser->renderString($this->validator->listErrors());
         $returnView .= $parser->setData($this->request->getPost())
         ->render('editplace');
         } else {
         $returnView .= $parser->renderString("<p>Success!</p>");
         $returnView .= $parser->setData($this->request->getPost())
         ->render('editplace');
        } 
        //Bottom of page
        $returnView .= $parser->setData($fields)
        ->render('templates\bottom');

        return $returnView;
}
}